package io.renren.modules.business.controller;


import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.modules.business.entity.EDepartmentEntity;
import io.renren.modules.business.service.EDepartmentService;
import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.renren.modules.business.entity.EApplyEntity;
import io.renren.modules.business.service.EApplyService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 申请表
 */
@RestController
@RequestMapping("/business/eapply")
public class EApplyController extends AbstractController {

    @Autowired
    private EApplyService eApplyService;

    @Autowired
    private EDepartmentService eDepartmentService;

    @Autowired
    private SysUserService sysUserService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = eApplyService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 查询申请列表
     * @param pageNum
     * @param pageSize
     * @param userId
     * @param applyStatus
     * @return
     */
    @GetMapping("/query")
    public R queryApplyList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                            @RequestParam(value = "userId", required = false) Long userId,
                            @RequestParam(value = "applyStatus", required = false) Integer applyStatus) {
        // 非管理员只能看到自己的
        if (getUserAuthority() != 1) {
            return R.ok().put("page", eApplyService.queryApplyList(pageNum, pageSize, getUserId(), applyStatus));
        }
        return R.ok().put("page", eApplyService.queryApplyList(pageNum, pageSize, userId, applyStatus));
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		EApplyEntity eApply = eApplyService.getById(id);

        return R.ok().put("eApply", eApply);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody EApplyEntity eApply) throws Exception {
        Long userId = eApply.getUserId();
        // 根据userId查询所在部门
        QueryWrapper<SysUserEntity> queryUserWrapper = new QueryWrapper<>();
        queryUserWrapper.eq("user_id", userId);
        List<SysUserEntity> userList = sysUserService.list(queryUserWrapper);
        if (userList.isEmpty() || userList.size() == 0) {
            throw new Exception("获取人员信息失败！");
        }
        Long departmentId = userList.get(0).getDepartmentId();
        // 根据部门查找审批人
        QueryWrapper<EDepartmentEntity> queryDepartmentWrapper = new QueryWrapper<>();
        queryDepartmentWrapper.eq("department_id", departmentId);
        List<EDepartmentEntity> departmentList = eDepartmentService.list(queryDepartmentWrapper);
        if (departmentList.isEmpty() || departmentList.size() == 0) {
            throw new Exception("获取部门信息失败！");
        }
        Long departmentLeaderId = departmentList.get(0).getDepartmentLeaderId();
        eApply.setApproveId(departmentLeaderId);
        eApplyService.save(eApply);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody EApplyEntity eApply){
		eApplyService.updateById(eApply);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestParam("id") Long id){
		eApplyService.removeById(id);
        return R.ok();
    }

}
